import 'package:flutter/material.dart';

class PracticeButton extends StatefulWidget {
  const PracticeButton({Key? key}) : super(key: key);

  @override
  _PracticeButtonState createState() => _PracticeButtonState();
}

class _PracticeButtonState extends State<PracticeButton> {
  int _sex = 1;

  String _username = '';

  List habbys = [
    {'choosed': true, 'name': '吃饭'},
    {'choosed': true, 'name': '睡觉'},
    {'choosed': false, 'name': '打豆豆'},
    {'choosed': false, 'name': '刷抖音'},
    {'choosed': false, 'name': '王者荣耀'},
  ];

  List<Widget> _getCheckboks() {
    List<Widget> views = [];

    // for (var i = 0; i < 3; ++i) {
    for (var i = 0; i < habbys.length; ++i) {
      // print('${habbys[i]['choosed']}');
      views.add(
        // CheckboxListTile(value: habbys[i]['choosed'], onChanged: (v) {
        //   setState(() {
        //     habbys[i]['choosed'] = v;
        //   });
        // }, title: Text(habbys[i]['name']),)
        // Container(
        //   child:
        Row(
          children: [
            //
            //       // CheckboxListTile(value: habbys[i]['choosed'], onChanged: (v) {
            //       //   setState(() {
            //       //     habbys[i]['choosed'] = v;
            //       //   });
            //       // }, title: Text(habbys[i]['name']),)
            Checkbox(
                value: habbys[i]['choosed'],
                onChanged: (value) {
                  setState(() {
                    habbys[i]['choosed'] = value;
                  });
                }),
            Text(habbys[i]['name'])
          ],
        ),
        // )
      );
    }

    return views;
    // return Wrap(
    //   children: views,
    // );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SingleChildScrollView(
      child: Column(
        children: [
          SizedBox(
            height: 40,
          ),
          Container(
            margin: EdgeInsets.all(20),
            child: TextField(
              maxLines: 5,
              decoration: InputDecoration(
                  focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: Colors.red, width: 1)),
                  enabledBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: Colors.grey, width: 1))),
            ),
          ),
          Container(
            margin: EdgeInsets.all(20),
            child: TextField(
              onChanged: (c) {
                _username = c;
              },
              decoration: InputDecoration(
                  hintText: "请输入用户名",
                  enabledBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: Colors.grey, width: 1))),
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text('男'),
              Radio(
                value: 1,
                groupValue: _sex,
                onChanged: (v) {
                  print('v::$v');
                  setState(() {
                    _sex = v as int;
                  });
                },
              ),
              SizedBox(
                width: 20,
              ),
              Text('女'),
              Radio(
                  value: 2,
                  groupValue: _sex,
                  onChanged: (v) {
                    setState(() {
                      _sex = v as int;
                    });
                  }),
            ],
          ),

          Wrap(children: _getCheckboks()),
          // _getCheckboks()

          Container(
            width: double.infinity,
            margin: EdgeInsets.only(left: 20, right: 20, top: 20),
            height: 45,
            child: ElevatedButton(
                onPressed: () {
                  print('用户名${_username}');
                  print('性别:' + (_sex == 1 ? "男" : "女"));
                  var hs = '';
                  for (var i = 0; i < habbys.length; ++i) {
                    // print(habbys[i]['choosed']);
                    // print(habbys[i]['name']);
                    if (habbys[i]['choosed']) {
                      hs = hs + habbys[i]['name'] + ",";
                    }
                  }

                  print('喜好${hs}');
                },
                child: Text('提交信息')),
          )
        ],
      ),
    ));
  }
}
